package com.gui.hosp.controller.api;

import com.gui.common.exception.YyghException;
import com.gui.common.helper.HttpRequestHelper;
import com.gui.common.result.Result;
import com.gui.common.result.ResultCodeEnum;
import com.gui.common.utils.MD5;
import com.gui.hosp.service.DepartmentService;
import com.gui.hosp.service.HospitalService;
import com.gui.hosp.service.HospitalSetService;
import com.gui.hosp.service.ScheduleService;
import com.gui.model.hosp.Department;
import com.gui.model.hosp.Hospital;
import com.gui.model.hosp.Schedule;
import com.gui.vo.hosp.DepartmentQueryVo;
import com.gui.vo.hosp.DepartmentVo;
import com.gui.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author GodLamp
 * @date 2022/10/18 13:11
 */
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping(path = "/api/hosp")
public class ApiController {
    @Resource
    private HospitalService hospitalService;

    @Resource
    private HospitalSetService hospitalSetService;

    @Resource
    private DepartmentService departmentService;

    @Resource
    private ScheduleService scheduleService;

    /**
     * 上传医院接口
     *
     * @param request
     * @return
     */
    @PostMapping(path = "saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        if (requestMap == null) {
            return Result.fail(ResultCodeEnum.PARAM_ERROR);
        }

        //转换map类型，便于后续操作
        Map<String, Object> params = HttpRequestHelper.switchMap(requestMap);

        //签名校验
        //获取后台传过来的sign
        String sign = (String) params.get("sign");
        //从数据库获取sign_key生成sign
        String hoscode = (String) params.get("hoscode");


        //解码过程中，logoData值的"+"被转换成了空格，所以这里需要转换回来
        String logoData = (String) params.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        params.put("logoData", logoData);

        //签名校验
        boolean resule = this.signValid(hoscode, sign);
        if (!resule) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        hospitalService.save(params);

        return Result.ok();
    }

    /**
     * 签名校验
     *
     * @param hoscode 医院编号
     * @param sign    传过来的已加密的签名密钥
     * @return
     */
    private boolean signValid(String hoscode, String sign) {
        if (sign == null || hoscode == null || "".equals(sign) || "".equals(hoscode)) {
            return false;
        }

        //查询数据库的签名密钥(yygh_hosp数据库的hospital_set表)
        String hosSign = hospitalSetService.getSignKey(hoscode);
        if (hosSign == null || "".equals(hosSign)) {
            return false;
        }

        //签名校验判断
        if (!MD5.encrypt(hosSign).equals(sign)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 查询医院
     *
     * @return
     */
    @PostMapping(path = "hospital/show")
    public Result getHospital(HttpServletRequest request) {
        Map<String, String[]> params = request.getParameterMap();
        Map<String, Object> maps = HttpRequestHelper.switchMap(params);

        String hoscode = (String) maps.get("hoscode");
        Long timestamp = Long.parseLong((String) maps.get("timestamp"));
        String sign = (String) maps.get("sign");

        if (!this.signValid(hoscode, sign)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        Hospital hospital = hospitalService.getByHoscode(hoscode);

        return Result.ok(hospital);
    }

    /**
     * 上传科室
     *
     * @return
     */
    @PostMapping(path = "saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        //获取传过来的科室信息
        Map<String, Object> params = HttpRequestHelper.switchMap(request.getParameterMap());

        if (!this.signValid((String) params.get("hoscode"), (String) params.get("sign"))) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.save(params);

        return Result.ok();
    }

    /**
     * 查询科室
     *
     * @param request
     * @return
     */
    @PostMapping(path = "department/list")
    public Result findDepartment(HttpServletRequest request) {
        Map<String, Object> params = HttpRequestHelper.switchMap(request.getParameterMap());

        String hoscode = (String) params.get("hoscode");

        int page = StringUtils.isEmpty(params.get("page")) ? 1 : Integer.parseInt((String) params.get("page"));
        int limit = StringUtils.isEmpty(params.get("limit")) ? 1 : Integer.parseInt((String) params.get("limit"));

        if (!this.signValid(hoscode, (String) params.get("sign"))) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        DepartmentQueryVo vo = new DepartmentQueryVo();
        vo.setHoscode(hoscode);

        Page<Department> pages = departmentService.findPageDepart(page, limit, vo);

        return Result.ok(pages);
    }

    /**
     * 删除指定科室
     *
     * @param request
     * @return
     */
    @PostMapping(path = "department/remove")
    public Result deleteDepartment(HttpServletRequest request) {
        Map<String, Object> params = HttpRequestHelper.switchMap(request.getParameterMap());

        if (!this.signValid((String) params.get("hoscode"), (String) params.get("sign"))) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        String hoscode = (String) params.get("hoscode");
        String depcode = (String) params.get("depcode");
        departmentService.remove(hoscode, depcode);

        return Result.ok();
    }

    /**
     * 上传排班信息
     * @param request
     * @return
     */
    @PostMapping(path = "saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, Object> params = HttpRequestHelper.switchMap(request.getParameterMap());

        if (!this.signValid((String) params.get("hoscode"), (String) params.get("sign"))) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.save(params);

        return Result.ok();
    }

    /**
     * 查询排班信息
     * @param request
     * @return
     */
    @PostMapping(path = "schedule/list")
    public Result findSchedule(HttpServletRequest request){
        Map<String, Object> params = HttpRequestHelper.switchMap(request.getParameterMap());

        if (!this.signValid((String) params.get("hoscode"), (String) params.get("sign"))) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        String hoscode = (String) params.get("hoscode");
        String depcode = (String) params.get("depcode");

        int page = StringUtils.isEmpty(params.get("page")) ? 1 : Integer.parseInt((String) params.get("page"));
        int limit = StringUtils.isEmpty(params.get("limit")) ? 1 : Integer.parseInt((String) params.get("limit"));

        ScheduleQueryVo vo = new ScheduleQueryVo();
        vo.setHoscode(hoscode);
        vo.setDepcode(depcode);

        Page<Schedule> schedules = scheduleService.findPageSchedule(page, limit, vo);

        return Result.ok(schedules);
    }

    /**
     * 删除排班信息
     * @param request
     * @return
     */
    @PostMapping(path = "schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String, Object> params = HttpRequestHelper.switchMap(request.getParameterMap());

        if (!this.signValid((String) params.get("hoscode"), (String) params.get("sign"))) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        String hoscode = (String) params.get("hoscode");
        String hosScheduleId = (String) params.get("hosScheduleId");

        scheduleService.remove(hoscode, hosScheduleId);

        return Result.ok();
    }
}
